﻿using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts.Standards.ERC721.ContractDefinition;
using Nethereum.Util;
using Nethereum.XUnitEthereumClients;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Threading;
using System.Threading.Tasks;
//using System.Threading.Tasks.Dataflow;
using Xunit;

namespace Nethereum.Contracts.IntegrationTests.SmartContracts.Standards
{
    [Collection(EthereumClientIntegrationFixture.ETHEREUM_CLIENT_COLLECTION_DEFAULT)]
    public class ERC721Tests
    {

        private readonly EthereumClientIntegrationFixture _ethereumClientIntegrationFixture;

        public ERC721Tests(EthereumClientIntegrationFixture ethereumClientIntegrationFixture)
        {
            _ethereumClientIntegrationFixture = ethereumClientIntegrationFixture;
        }

        public partial class MyERC721Deployment : MyERC721DeploymentBase
        {
            public MyERC721Deployment() : base(BYTECODE)
            {
            }

            public MyERC721Deployment(string byteCode) : base(byteCode)
            {
            }
        }

        public class MyERC721DeploymentBase : ContractDeploymentMessage
        {
            public static string BYTECODE =
                "6101406040523480156200001257600080fd5b506040516200332a3803806200332a8339810160408190526200003591620002ff565b81604051806040016040528060018152602001603160f81b815250838381600090805190602001906200006a9291906200018c565b508051620000809060019060208401906200018c565b5050600b805460ff1916905550620000983362000132565b815160208084019190912082518383012060e08290526101008190524660a0818152604080517f8b73c3c69bb8fe3d512ecc4cf759cc79239f7b179b0ffacaa9a75d522b39400f81880181905281830187905260608201869052608082019490945230818401528151808203909301835260c00190528051940193909320919290916080523060c0526101205250620003a5945050505050565b600b80546001600160a01b03838116610100818102610100600160a81b031985161790945560405193909204169182907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b8280546200019a9062000369565b90600052602060002090601f016020900481019282620001be576000855562000209565b82601f10620001d957805160ff191683800117855562000209565b8280016001018555821562000209579182015b8281111562000209578251825591602001919060010190620001ec565b50620002179291506200021b565b5090565b5b808211156200021757600081556001016200021c565b634e487b7160e01b600052604160045260246000fd5b600082601f8301126200025a57600080fd5b81516001600160401b038082111562000277576200027762000232565b604051601f8301601f19908116603f01168101908282118183101715620002a257620002a262000232565b81604052838152602092508683858801011115620002bf57600080fd5b600091505b83821015620002e35785820183015181830184015290820190620002c4565b83821115620002f55760008385830101525b9695505050505050565b600080604083850312156200031357600080fd5b82516001600160401b03808211156200032b57600080fd5b620003398683870162000248565b935060208501519150808211156200035057600080fd5b506200035f8582860162000248565b9150509250929050565b600181811c908216806200037e57607f821691505b6020821081036200039f57634e487b7160e01b600052602260045260246000fd5b50919050565b60805160a05160c05160e0516101005161012051612f35620003f560003960006111a4015260006111f3015260006111ce01526000611127015260006111510152600061117b0152612f356000f3fe608060405234801561001057600080fd5b50600436106101f05760003560e01c80636352211e1161010f5780639ab24eb0116100a2578063c87b56dd11610071578063c87b56dd14610416578063d204c45e14610429578063e985e9c51461043c578063f2fde38b1461047857600080fd5b80639ab24eb0146103ca578063a22cb465146103dd578063b88d4fde146103f0578063c3cda5201461040357600080fd5b80638456cb59116100de5780638456cb59146103915780638da5cb5b146103995780638e539e8c146103af57806395d89b41146103c257600080fd5b80636352211e1461035057806370a0823114610363578063715018a6146103765780637ecebe001461037e57600080fd5b80633a46b1a8116101875780634f6ccce7116101565780634f6ccce7146102f3578063587cde1e146103065780635c19a95c146103325780635c975abb1461034557600080fd5b80633a46b1a8146102b25780633f4ba83a146102c557806342842e0e146102cd57806342966c68146102e057600080fd5b806318160ddd116101c357806318160ddd1461027257806323b872dd146102845780632f745c59146102975780633644e515146102aa57600080fd5b806301ffc9a7146101f557806306fdde031461021d578063081812fc14610232578063095ea7b31461025d575b600080fd5b610208610203366004612915565b61048b565b60405190151581526020015b60405180910390f35b61022561049c565b604051610214919061298a565b61024561024036600461299d565b61052e565b6040516001600160a01b039091168152602001610214565b61027061026b3660046129d2565b6105bb565b005b6008545b604051908152602001610214565b6102706102923660046129fc565b6106d0565b6102766102a53660046129d2565b610702565b610276610798565b6102766102c03660046129d2565b6107a7565b6102706107d0565b6102706102db3660046129fc565b61080a565b6102706102ee36600461299d565b610825565b61027661030136600461299d565b61089f565b610245610314366004612a38565b6001600160a01b039081166000908152600c60205260409020541690565b610270610340366004612a38565b610932565b600b5460ff16610208565b61024561035e36600461299d565b610941565b610276610371366004612a38565b6109b8565b610270610a3f565b61027661038c366004612a38565b610a79565b610270610a97565b600b5461010090046001600160a01b0316610245565b6102766103bd36600461299d565b610acf565b610225610b2b565b6102766103d8366004612a38565b610b3a565b6102706103eb366004612a53565b610b5b565b6102706103fe366004612b1b565b610b66565b610270610411366004612b97565b610b9e565b61022561042436600461299d565b610ccb565b610270610437366004612bf7565b610cd6565b61020861044a366004612c59565b6001600160a01b03918216600090815260056020908152604080832093909416825291909152205460ff1690565b610270610486366004612a38565b610d35565b600061049682610dd3565b92915050565b6060600080546104ab90612c8c565b80601f01602080910402602001604051908101604052809291908181526020018280546104d790612c8c565b80156105245780601f106104f957610100808354040283529160200191610524565b820191906000526020600020905b81548152906001019060200180831161050757829003601f168201915b5050505050905090565b600061053982610df8565b61059f5760405162461bcd60e51b815260206004820152602c60248201527f4552433732313a20617070726f76656420717565727920666f72206e6f6e657860448201526b34b9ba32b73a103a37b5b2b760a11b60648201526084015b60405180910390fd5b506000908152600460205260409020546001600160a01b031690565b60006105c682610941565b9050806001600160a01b0316836001600160a01b0316036106335760405162461bcd60e51b815260206004820152602160248201527f4552433732313a20617070726f76616c20746f2063757272656e74206f776e656044820152603960f91b6064820152608401610596565b336001600160a01b038216148061064f575061064f813361044a565b6106c15760405162461bcd60e51b815260206004820152603860248201527f4552433732313a20617070726f76652063616c6c6572206973206e6f74206f7760448201527f6e6572206e6f7220617070726f76656420666f7220616c6c00000000000000006064820152608401610596565b6106cb8383610e15565b505050565b6106db335b82610e83565b6106f75760405162461bcd60e51b815260040161059690612cc0565b6106cb838383610f6d565b600061070d836109b8565b821061076f5760405162461bcd60e51b815260206004820152602b60248201527f455243373231456e756d657261626c653a206f776e657220696e646578206f7560448201526a74206f6620626f756e647360a81b6064820152608401610596565b506001600160a01b03919091166000908152600660209081526040808320938352929052205490565b60006107a261111a565b905090565b6001600160a01b0382166000908152600d602052604081206107c99083611241565b9392505050565b600b546001600160a01b036101009091041633146108005760405162461bcd60e51b815260040161059690612d11565b610808611350565b565b6106cb83838360405180602001604052806000815250610b66565b61082e336106d5565b6108935760405162461bcd60e51b815260206004820152603060248201527f4552433732314275726e61626c653a2063616c6c6572206973206e6f74206f7760448201526f1b995c881b9bdc88185c1c1c9bdd995960821b6064820152608401610596565b61089c816113e3565b50565b60006108aa60085490565b821061090d5760405162461bcd60e51b815260206004820152602c60248201527f455243373231456e756d657261626c653a20676c6f62616c20696e646578206f60448201526b7574206f6620626f756e647360a01b6064820152608401610596565b6008828154811061092057610920612d46565b90600052602060002001549050919050565b3361093d81836113ec565b5050565b6000818152600260205260408120546001600160a01b0316806104965760405162461bcd60e51b815260206004820152602960248201527f4552433732313a206f776e657220717565727920666f72206e6f6e657869737460448201526832b73a103a37b5b2b760b91b6064820152608401610596565b60006001600160a01b038216610a235760405162461bcd60e51b815260206004820152602a60248201527f4552433732313a2062616c616e636520717565727920666f7220746865207a65604482015269726f206164647265737360b01b6064820152608401610596565b506001600160a01b031660009081526003602052604090205490565b600b546001600160a01b03610100909104163314610a6f5760405162461bcd60e51b815260040161059690612d11565b610808600061145e565b6001600160a01b0381166000908152600f6020526040812054610496565b600b546001600160a01b03610100909104163314610ac75760405162461bcd60e51b815260040161059690612d11565b6108086114b8565b6000438210610b205760405162461bcd60e51b815260206004820152601a60248201527f566f7465733a20626c6f636b206e6f7420796574206d696e65640000000000006044820152606401610596565b610496600e83611241565b6060600180546104ab90612c8c565b6001600160a01b0381166000908152600d6020526040812061049690611533565b61093d33838361158f565b610b703383610e83565b610b8c5760405162461bcd60e51b815260040161059690612cc0565b610b988484848461165d565b50505050565b83421115610bee5760405162461bcd60e51b815260206004820152601860248201527f566f7465733a207369676e6174757265206578706972656400000000000000006044820152606401610596565b604080517fe48329057bfd03d55e49b547132e39cffd9c1820ad7b9d4c5307691425d15adf60208201526001600160a01b038816918101919091526060810186905260808101859052600090610c6890610c609060a00160405160208183030381529060405280519060200120611690565b8585856116de565b9050610c7381611706565b8614610cb85760405162461bcd60e51b8152602060048201526014602482015273566f7465733a20696e76616c6964206e6f6e636560601b6044820152606401610596565b610cc281886113ec565b50505050505050565b60606104968261172e565b600b546001600160a01b03610100909104163314610d065760405162461bcd60e51b815260040161059690612d11565b6000610d1160105490565b9050610d21601080546001019055565b610d2b838261189c565b6106cb81836118b6565b600b546001600160a01b03610100909104163314610d655760405162461bcd60e51b815260040161059690612d11565b6001600160a01b038116610dca5760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b6064820152608401610596565b61089c8161145e565b60006001600160e01b0319821663780e9d6360e01b1480610496575061049682611941565b6000908152600260205260409020546001600160a01b0316151590565b600081815260046020526040902080546001600160a01b0319166001600160a01b0384169081179091558190610e4a82610941565b6001600160a01b03167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560405160405180910390a45050565b6000610e8e82610df8565b610eef5760405162461bcd60e51b815260206004820152602c60248201527f4552433732313a206f70657261746f7220717565727920666f72206e6f6e657860448201526b34b9ba32b73a103a37b5b2b760a11b6064820152608401610596565b6000610efa83610941565b9050806001600160a01b0316846001600160a01b03161480610f355750836001600160a01b0316610f2a8461052e565b6001600160a01b0316145b80610f6557506001600160a01b0380821660009081526005602090815260408083209388168352929052205460ff165b949350505050565b826001600160a01b0316610f8082610941565b6001600160a01b031614610fe45760405162461bcd60e51b815260206004820152602560248201527f4552433732313a207472616e736665722066726f6d20696e636f72726563742060448201526437bbb732b960d91b6064820152608401610596565b6001600160a01b0382166110465760405162461bcd60e51b8152602060048201526024808201527f4552433732313a207472616e7366657220746f20746865207a65726f206164646044820152637265737360e01b6064820152608401610596565b611051838383611991565b61105c600082610e15565b6001600160a01b0383166000908152600360205260408120805460019290611085908490612d72565b90915550506001600160a01b03821660009081526003602052604081208054600192906110b3908490612d89565b909155505060008181526002602052604080822080546001600160a01b0319166001600160a01b0386811691821790925591518493918716917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef91a46106cb8383836119e2565b6000306001600160a01b037f00000000000000000000000000000000000000000000000000000000000000001614801561117357507f000000000000000000000000000000000000000000000000000000000000000046145b1561119d57507f000000000000000000000000000000000000000000000000000000000000000090565b50604080517f00000000000000000000000000000000000000000000000000000000000000006020808301919091527f0000000000000000000000000000000000000000000000000000000000000000828401527f000000000000000000000000000000000000000000000000000000000000000060608301524660808301523060a0808401919091528351808403909101815260c0909201909252805191012090565b60004382106112925760405162461bcd60e51b815260206004820181905260248201527f436865636b706f696e74733a20626c6f636b206e6f7420796574206d696e65646044820152606401610596565b825460005b818110156112f75760006112ab82846119ed565b9050848660000182815481106112c3576112c3612d46565b60009182526020909120015463ffffffff1611156112e3578092506112f1565b6112ee816001612d89565b91505b50611297565b811561133b5784611309600184612d72565b8154811061131957611319612d46565b60009182526020909120015464010000000090046001600160e01b031661133e565b60005b6001600160e01b031695945050505050565b600b5460ff166113995760405162461bcd60e51b815260206004820152601460248201527314185d5cd8589b194e881b9bdd081c185d5cd95960621b6044820152606401610596565b600b805460ff191690557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa335b6040516001600160a01b03909116815260200160405180910390a1565b61089c81611a08565b6001600160a01b038281166000818152600c602052604080822080548686166001600160a01b0319821681179092559151919094169392849290917f3134e8a2e6d97e929a7e54011ea5485d7d196dd5f0ba4d4ef95803e8e3fc257f9190a46106cb818361145986611a48565b611a53565b600b80546001600160a01b03838116610100818102610100600160a81b031985161790945560405193909204169182907f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e090600090a35050565b600b5460ff16156114fe5760405162461bcd60e51b815260206004820152601060248201526f14185d5cd8589b194e881c185d5cd95960821b6044820152606401610596565b600b805460ff191660011790557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a2586113c63390565b8054600090801561157c578261154a600183612d72565b8154811061155a5761155a612d46565b60009182526020909120015464010000000090046001600160e01b031661157f565b60005b6001600160e01b03169392505050565b816001600160a01b0316836001600160a01b0316036115f05760405162461bcd60e51b815260206004820152601960248201527f4552433732313a20617070726f766520746f2063616c6c6572000000000000006044820152606401610596565b6001600160a01b03838116600081815260056020908152604080832094871680845294825291829020805460ff191686151590811790915591519182527f17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c31910160405180910390a3505050565b611668848484610f6d565b61167484848484611b90565b610b985760405162461bcd60e51b815260040161059690612da1565b600061049661169d61111a565b8360405161190160f01b6020820152602281018390526042810182905260009060620160405160208183030381529060405280519060200120905092915050565b60008060006116ef87878787611c8e565b915091506116fc81611d7b565b5095945050505050565b6001600160a01b0381166000908152600f602052604090208054600181018255905b50919050565b606061173982610df8565b61179f5760405162461bcd60e51b815260206004820152603160248201527f45524337323155524953746f726167653a2055524920717565727920666f72206044820152703737b732bc34b9ba32b73a103a37b5b2b760791b6064820152608401610596565b6000828152600a6020526040812080546117b890612c8c565b80601f01602080910402602001604051908101604052809291908181526020018280546117e490612c8c565b80156118315780601f1061180657610100808354040283529160200191611831565b820191906000526020600020905b81548152906001019060200180831161181457829003601f168201915b50505050509050600061184f60408051602081019091526000815290565b90508051600003611861575092915050565b81511561189357808260405160200161187b929190612df3565b60405160208183030381529060405292505050919050565b610f6584611f31565b61093d828260405180602001604052806000815250612008565b6118bf82610df8565b6119225760405162461bcd60e51b815260206004820152602e60248201527f45524337323155524953746f726167653a2055524920736574206f66206e6f6e60448201526d32bc34b9ba32b73a103a37b5b2b760911b6064820152608401610596565b6000828152600a6020908152604090912082516106cb92840190612834565b60006001600160e01b031982166380ac58cd60e01b148061197257506001600160e01b03198216635b5e139f60e01b145b8061049657506301ffc9a760e01b6001600160e01b0319831614610496565b600b5460ff16156119d75760405162461bcd60e51b815260206004820152601060248201526f14185d5cd8589b194e881c185d5cd95960821b6044820152606401610596565b6106cb83838361203b565b6106cb8383836120f3565b60006119fc6002848418612e38565b6107c990848416612d89565b611a11816120ff565b6000818152600a602052604090208054611a2a90612c8c565b15905061089c576000818152600a6020526040812061089c916128b4565b6000610496826109b8565b816001600160a01b0316836001600160a01b031614158015611a755750600081115b156106cb576001600160a01b03831615611b03576001600160a01b0383166000908152600d602052604081208190611ab0906121ae856121ba565b91509150846001600160a01b03167fdec2bacdd2f05b59de34da9b523dff8be42e5e38e818c82fdb0bae774387a7248383604051611af8929190918252602082015260400190565b60405180910390a250505b6001600160a01b038216156106cb576001600160a01b0382166000908152600d602052604081208190611b39906121e8856121ba565b91509150836001600160a01b03167fdec2bacdd2f05b59de34da9b523dff8be42e5e38e818c82fdb0bae774387a7248383604051611b81929190918252602082015260400190565b60405180910390a25050505050565b60006001600160a01b0384163b15611c8657604051630a85bd0160e11b81526001600160a01b0385169063150b7a0290611bd4903390899088908890600401612e4c565b6020604051808303816000875af1925050508015611c0f575060408051601f3d908101601f19168201909252611c0c91810190612e89565b60015b611c6c573d808015611c3d576040519150601f19603f3d011682016040523d82523d6000602084013e611c42565b606091505b508051600003611c645760405162461bcd60e51b815260040161059690612da1565b805181602001fd5b6001600160e01b031916630a85bd0160e11b149050610f65565b506001610f65565b6000807f7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0831115611cc55750600090506003611d72565b8460ff16601b14158015611cdd57508460ff16601c14155b15611cee5750600090506004611d72565b6040805160008082526020820180845289905260ff881692820192909252606081018690526080810185905260019060a0016020604051602081039080840390855afa158015611d42573d6000803e3d6000fd5b5050604051601f1901519150506001600160a01b038116611d6b57600060019250925050611d72565b9150600090505b94509492505050565b6000816004811115611d8f57611d8f612ea6565b03611d975750565b6001816004811115611dab57611dab612ea6565b03611df85760405162461bcd60e51b815260206004820152601860248201527f45434453413a20696e76616c6964207369676e617475726500000000000000006044820152606401610596565b6002816004811115611e0c57611e0c612ea6565b03611e595760405162461bcd60e51b815260206004820152601f60248201527f45434453413a20696e76616c6964207369676e6174757265206c656e677468006044820152606401610596565b6003816004811115611e6d57611e6d612ea6565b03611ec55760405162461bcd60e51b815260206004820152602260248201527f45434453413a20696e76616c6964207369676e6174757265202773272076616c604482015261756560f01b6064820152608401610596565b6004816004811115611ed957611ed9612ea6565b0361089c5760405162461bcd60e51b815260206004820152602260248201527f45434453413a20696e76616c6964207369676e6174757265202776272076616c604482015261756560f01b6064820152608401610596565b6060611f3c82610df8565b611fa05760405162461bcd60e51b815260206004820152602f60248201527f4552433732314d657461646174613a2055524920717565727920666f72206e6f60448201526e3732bc34b9ba32b73a103a37b5b2b760891b6064820152608401610596565b6000611fb760408051602081019091526000815290565b90506000815111611fd757604051806020016040528060008152506107c9565b80611fe1846121f4565b604051602001611ff2929190612df3565b6040516020818303038152906040529392505050565b61201283836122f5565b61201f6000848484611b90565b6106cb5760405162461bcd60e51b815260040161059690612da1565b6001600160a01b0383166120965761209181600880546000838152600960205260408120829055600182018355919091527ff3f7a9fe364faab93b216da50a3214154f22a0a2b415b23a84c8169e8b636ee30155565b6120b9565b816001600160a01b0316836001600160a01b0316146120b9576120b9838261243c565b6001600160a01b0382166120d0576106cb816124d9565b826001600160a01b0316826001600160a01b0316146106cb576106cb8282612588565b6106cb838360016125cc565b600061210a82610941565b905061211881600084611991565b612123600083610e15565b6001600160a01b038116600090815260036020526040812080546001929061214c908490612d72565b909155505060008281526002602052604080822080546001600160a01b0319169055518391906001600160a01b038416907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef908390a461093d816000846119e2565b60006107c98284612d72565b6000806121dc856121d76121cd88611533565b868863ffffffff16565b61263c565b91509150935093915050565b60006107c98284612d89565b60608160000361221b5750506040805180820190915260018152600360fc1b602082015290565b8160005b8115612245578061222f81612ebc565b915061223e9050600a83612e38565b915061221f565b60008167ffffffffffffffff81111561226057612260612a8f565b6040519080825280601f01601f19166020018201604052801561228a576020820181803683370190505b5090505b8415610f655761229f600183612d72565b91506122ac600a86612ed5565b6122b7906030612d89565b60f81b8183815181106122cc576122cc612d46565b60200101906001600160f81b031916908160001a9053506122ee600a86612e38565b945061228e565b6001600160a01b03821661234b5760405162461bcd60e51b815260206004820181905260248201527f4552433732313a206d696e7420746f20746865207a65726f20616464726573736044820152606401610596565b61235481610df8565b156123a15760405162461bcd60e51b815260206004820152601c60248201527f4552433732313a20746f6b656e20616c7265616479206d696e746564000000006044820152606401610596565b6123ad60008383611991565b6001600160a01b03821660009081526003602052604081208054600192906123d6908490612d89565b909155505060008181526002602052604080822080546001600160a01b0319166001600160a01b03861690811790915590518392907fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef908290a461093d600083836119e2565b60006001612449846109b8565b6124539190612d72565b6000838152600760205260409020549091508082146124a6576001600160a01b03841660009081526006602090815260408083208584528252808320548484528184208190558352600790915290208190555b5060009182526007602090815260408084208490556001600160a01b039094168352600681528383209183525290812055565b6008546000906124eb90600190612d72565b6000838152600960205260408120546008805493945090928490811061251357612513612d46565b90600052602060002001549050806008838154811061253457612534612d46565b600091825260208083209091019290925582815260099091526040808220849055858252812055600880548061256c5761256c612ee9565b6001900381819060005260206000200160009055905550505050565b6000612593836109b8565b6001600160a01b039093166000908152600660209081526040808320868452825280832085905593825260079052919091209190915550565b6001600160a01b0383166125eb576125e8600e6121e8836121ba565b50505b6001600160a01b03821661260a57612607600e6121ae836121ba565b50505b6001600160a01b038381166000908152600c60205260408082205485841683529120546106cb92918216911683611a53565b815460009081908161264d86611533565b905060008211801561268b57504386612667600185612d72565b8154811061267757612677612d46565b60009182526020909120015463ffffffff16145b156126eb5761269985612762565b866126a5600185612d72565b815481106126b5576126b5612d46565b9060005260206000200160000160046101000a8154816001600160e01b0302191690836001600160e01b03160217905550612759565b856000016040518060400160405280612703436127cf565b63ffffffff16815260200161271788612762565b6001600160e01b0390811690915282546001810184556000938452602093849020835194909301519091166401000000000263ffffffff909316929092179101555b95939450505050565b60006001600160e01b038211156127cb5760405162461bcd60e51b815260206004820152602760248201527f53616665436173743a2076616c756520646f65736e27742066697420696e20326044820152663234206269747360c81b6064820152608401610596565b5090565b600063ffffffff8211156127cb5760405162461bcd60e51b815260206004820152602660248201527f53616665436173743a2076616c756520646f65736e27742066697420696e203360448201526532206269747360d01b6064820152608401610596565b82805461284090612c8c565b90600052602060002090601f01602090048101928261286257600085556128a8565b82601f1061287b57805160ff19168380011785556128a8565b828001600101855582156128a8579182015b828111156128a857825182559160200191906001019061288d565b506127cb9291506128ea565b5080546128c090612c8c565b6000825580601f106128d0575050565b601f01602090049060005260206000209081019061089c91905b5b808211156127cb57600081556001016128eb565b6001600160e01b03198116811461089c57600080fd5b60006020828403121561292757600080fd5b81356107c9816128ff565b60005b8381101561294d578181015183820152602001612935565b83811115610b985750506000910152565b60008151808452612976816020860160208601612932565b601f01601f19169290920160200192915050565b6020815260006107c9602083018461295e565b6000602082840312156129af57600080fd5b5035919050565b80356001600160a01b03811681146129cd57600080fd5b919050565b600080604083850312156129e557600080fd5b6129ee836129b6565b946020939093013593505050565b600080600060608486031215612a1157600080fd5b612a1a846129b6565b9250612a28602085016129b6565b9150604084013590509250925092565b600060208284031215612a4a57600080fd5b6107c9826129b6565b60008060408385031215612a6657600080fd5b612a6f836129b6565b915060208301358015158114612a8457600080fd5b809150509250929050565b634e487b7160e01b600052604160045260246000fd5b600067ffffffffffffffff80841115612ac057612ac0612a8f565b604051601f8501601f19908116603f01168101908282118183101715612ae857612ae8612a8f565b81604052809350858152868686011115612b0157600080fd5b858560208301376000602087830101525050509392505050565b60008060008060808587031215612b3157600080fd5b612b3a856129b6565b9350612b48602086016129b6565b925060408501359150606085013567ffffffffffffffff811115612b6b57600080fd5b8501601f81018713612b7c57600080fd5b612b8b87823560208401612aa5565b91505092959194509250565b60008060008060008060c08789031215612bb057600080fd5b612bb9876129b6565b95506020870135945060408701359350606087013560ff81168114612bdd57600080fd5b9598949750929560808101359460a0909101359350915050565b60008060408385031215612c0a57600080fd5b612c13836129b6565b9150602083013567ffffffffffffffff811115612c2f57600080fd5b8301601f81018513612c4057600080fd5b612c4f85823560208401612aa5565b9150509250929050565b60008060408385031215612c6c57600080fd5b612c75836129b6565b9150612c83602084016129b6565b90509250929050565b600181811c90821680612ca057607f821691505b60208210810361172857634e487b7160e01b600052602260045260246000fd5b60208082526031908201527f4552433732313a207472616e736665722063616c6c6572206973206e6f74206f6040820152701ddb995c881b9bdc88185c1c1c9bdd9959607a1b606082015260800190565b6020808252818101527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604082015260600190565b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052601160045260246000fd5b600082821015612d8457612d84612d5c565b500390565b60008219821115612d9c57612d9c612d5c565b500190565b60208082526032908201527f4552433732313a207472616e7366657220746f206e6f6e20455243373231526560408201527131b2b4bb32b91034b6b83632b6b2b73a32b960711b606082015260800190565b60008351612e05818460208801612932565b835190830190612e19818360208801612932565b01949350505050565b634e487b7160e01b600052601260045260246000fd5b600082612e4757612e47612e22565b500490565b6001600160a01b0385811682528416602082015260408101839052608060608201819052600090612e7f9083018461295e565b9695505050505050565b600060208284031215612e9b57600080fd5b81516107c9816128ff565b634e487b7160e01b600052602160045260246000fd5b600060018201612ece57612ece612d5c565b5060010190565b600082612ee457612ee4612e22565b500690565b634e487b7160e01b600052603160045260246000fdfea2646970667358221220820f375dbdbbacb4510f8a05836cdd93fc977f757ea9e6b6c50fe455cb6190e964736f6c634300080d0033";

            public MyERC721DeploymentBase() : base(BYTECODE)
            {
            }

            public MyERC721DeploymentBase(string byteCode) : base(byteCode)
            {
            }

            [Parameter("string", "name", 1)] public virtual string Name { get; set; }
            [Parameter("string", "symbol", 2)] public virtual string Symbol { get; set; }
        }



        [Fact]
        public async void ShouldDeployCustomAndQueryInteractWithGenericService()
        {

            var web3 = _ethereumClientIntegrationFixture.GetWeb3();
            web3.Eth.TransactionManager.UseLegacyAsDefault = true;


            var erc721Deployment = new MyERC721Deployment() {Name = "Property Registry", Symbol = "PR"};

            var deploymentReceipt = await web3.Eth.GetContractDeploymentHandler<MyERC721Deployment>()
                .SendRequestAndWaitForReceiptAsync(erc721Deployment).ConfigureAwait(false);

            //creating a new service with the new contract address
            var erc721Service = web3.Eth.ERC721.GetContractService(deploymentReceipt.ContractAddress);
            var url = "ipfs://bafkreiblz4ltiepapdhqhjiurmfpov7extmxwcntqskvx2zqisoftlmk7a";

            var addressToRegisterOwnership = "0xe612205919814b1995D861Bdf6C2fE2f20cDBd68";

            var mintReceipt =
                await erc721Service.SafeMintRequestAndWaitForReceiptAsync(web3.TransactionManager.Account.Address, url).ConfigureAwait(false);

            var ownerOfToken = await erc721Service.OwnerOfQueryAsync(0).ConfigureAwait(false);

            Assert.True(ownerOfToken.IsTheSameAddress(web3.TransactionManager.Account.Address));

            var addressOfToken = await erc721Service.TokenURIQueryAsync(0).ConfigureAwait(false);

            Assert.Equal(url, addressOfToken);

            var transfer =
                await erc721Service.TransferFromRequestAndWaitForReceiptAsync(ownerOfToken, addressToRegisterOwnership,
                    0).ConfigureAwait(false);
            Assert.False(transfer.HasErrors());

            ownerOfToken = await erc721Service.OwnerOfQueryAsync(0).ConfigureAwait(false);
            Assert.True(ownerOfToken.IsTheSameAddress(addressToRegisterOwnership));

        }

        [Fact] //
        public async void ShouldRetriveAllTheMetadataUrls()
        {
            //e2e test of using multicall to retrieve all the ipfs metadata urls of a user
            //var web3 = new Web3.Web3("https://optimism-mainnet.infura.io/v3/<<infuraId>>");
            var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Mainnet);
            var contractAddress =
                "0x31385d3520bced94f77aae104b406994d8f2168c";
            var erc721Service = web3.Eth.ERC721.GetContractService(contractAddress);
            var ownerToCheck = await erc721Service.OwnerOfQueryAsync(1).ConfigureAwait(false);
            //var erc721Service = web3.Eth.ERC721.GetContractService("0xB8Df6Cc3050cC02F967Db1eE48330bA23276A492"); //optimism
            var urls = await erc721Service.GetAllTokenUrlsOfOwnerUsingTokenOfOwnerByIndexAndMultiCallAsync(
                ownerToCheck).ConfigureAwait(false); //Pick an owner with some balance
            Assert.True(urls.Any());


        }

        // [Fact]
        ///This is slow as it uses bactches of 3000 calls to get all owned tokens of 11305
        public async void ShouldRetrieveAllOwnersUsingIdRangeAndMulticallAsync()
        {
            var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Mainnet);
            //creating a new service with the new contract address
            var contractAddress =
                "0x31385d3520bced94f77aae104b406994d8f2168c";
            var erc721Service = web3.Eth.ERC721.GetContractService(contractAddress);
            var owners = await web3.Eth.ERC721.GetContractService(contractAddress)
                .GetAllOwnersUsingIdRangeAndMultiCallAsync(0, 11304).ConfigureAwait(false);
            Assert.Equal(11305, owners.Count);

        }

       
        [Fact]
        public async void ShouldRetriveAllOwnnedTokensUsingProcessor()
        {
            var web3 = _ethereumClientIntegrationFixture.GetInfuraWeb3(InfuraNetwork.Mainnet);

            //creating a new service with the new contract address
            var contractAddresses = new string[]
                {"0xd24a7c412f2279b1901e591898c1e96c140be8c5", "0x31385d3520bced94f77aae104b406994d8f2168c"};
            var erc721Service = web3.Eth.ERC721.GetContractService(contractAddresses[0]);
            var ownerToCheck = await erc721Service.OwnerOfQueryAsync(1).ConfigureAwait(false);

            var cancellationToken = new CancellationToken();
            //// this is 143 calls as we hit the limit
            //var allOwners =
            //    await GetAllCurrentOwnersProcessingTransferEvents(web3, contractAddresses, null, null,
            //        cancellationToken);

            var ownedByAccount = await web3.Processing.Logs.ERC721.GetErc721OwnedByAccountUsingAllTransfersForContract(
                contractAddresses[0],
                ownerToCheck, null, null, cancellationToken).ConfigureAwait(false);
            Assert.True(ownedByAccount.Count > 0);
            var ownedByAccount2 =
                await web3.Processing.Logs.ERC721.GetErc721OwnedByAccountUsingAllTransfersForContracts(
                    contractAddresses,
                    ownerToCheck, null, null, cancellationToken).ConfigureAwait(false);
            Assert.True(ownedByAccount2.Count >= ownedByAccount.Count);
        }

        //[Fact]
        public async void ShouldGetAllApeOwners()
        {
            var web3 = new Web3.Web3("http://fullnode.dappnode:8545");
            Nethereum.JsonRpc.Client.ClientBase.ConnectionTimeout = TimeSpan.FromSeconds(190.0);
            var erc721TokenContractAddress = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";


            var tokensOwned = await web3.Processing.Logs.ERC721.GetAllCurrentOwnersProcessingAllTransferEvents(erc721TokenContractAddress, 12287507, null, default(CancellationToken), 50000).ConfigureAwait(false);

            foreach (var token in tokensOwned)
            {
                System.Diagnostics.Debug.WriteLine(token.Owner);
                System.Diagnostics.Debug.WriteLine(token.TokenId);
            }
        }


        //[Fact]
    //    public async void ShouldGetAllApeOwners2()
    //    {
    //        var web3 = new Web3.Web3("http://fullnode.dappnode:8545");
    //        Nethereum.JsonRpc.Client.ClientBase.ConnectionTimeout = TimeSpan.FromSeconds(190.0);
    //        var erc721TokenContractAddress = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D";
    //        var currentBlockNumber = await web3.Eth.Blocks.GetBlockNumber.SendRequestAsync();
    //        var numberOfBlocks = 50000;
    //        var blockRanges = GetBlockRanges(12287507, currentBlockNumber.Value, numberOfBlocks);
    //        var items = new ConcurrentBag<EventLog<TransferEventDTO>>();
    //        await blockRanges
    //            .ParallelForEachAsync(
    //                async item =>
    //                {
    //                    var transfers = await web3.Processing.Logs.ERC721.GetAllTransferEventsForContract(erc721TokenContractAddress, item.StartBlockNumber, item.EndBlockNumber, default(CancellationToken), numberOfBlocks); ;
    //                    foreach(var transfer in transfers)
    //                    {
    //                        items.Add(transfer);
    //                    };
    //                },
    //                3
    //                //Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.75) * 2.0))
    //            );

    //        //foreach (var token in tokensOwned)
    //        //{
    //        //    //System.Diagnostics.Debug.WriteLine(token.Owner);
    //        //   // System.Diagnostics.Debug.WriteLine(token.TokenId);
    //        //}
    //    }

       

    //    public List<BlockRange> GetBlockRanges(BigInteger startBlockNumber, BigInteger endBlockNumber, int pageSize)
    //    {
    //        var currentBlockNumber = startBlockNumber;
    //        var blockRanges = new List<BlockRange>();
            
    //        while (currentBlockNumber < endBlockNumber)
    //        {
    //            blockRanges.Add(new BlockRange() { StartBlockNumber = currentBlockNumber, EndBlockNumber = currentBlockNumber + pageSize });
    //            currentBlockNumber = currentBlockNumber + pageSize;
    //        }

    //        return blockRanges;
    //    }


    //    public class BlockRange
    //    {
    //        public BigInteger StartBlockNumber { get; set; }
    //        public BigInteger EndBlockNumber { get; set; }
    //    }
    //}


    //public static class AsyncExtensions
    //{
    //    public static Task ParallelForEachAsync<T>(this IEnumerable<T> source, Func<T, Task> body, int maxDop = DataflowBlockOptions.Unbounded, TaskScheduler scheduler = null)
    //    {
    //        var options = new ExecutionDataflowBlockOptions
    //        {
    //            MaxDegreeOfParallelism = maxDop
    //        };
    //        if (scheduler != null)
    //            options.TaskScheduler = scheduler;

    //        var block = new ActionBlock<T>(body, options);

    //        foreach (var item in source)
    //            block.Post(item);

    //        block.Complete();
    //        return block.Completion;
    //    }
    }
}